<?php
/**
 * 我的商城
 *
 *
 *
 *
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class member_indexControl extends mobileMemberControl {

	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 个人中心消息
	 */
	public function msg_countOp(){
	    
	    $wait_pay=0;
	    $wait_send=0;
	    $wait_receive=0;
	    $wait_evaluate=0;
	    
	    //过滤拼团订单
	    $condition['team_id']=0;
	    //代付款，代发货，待收货
	    //$condition['order_state']=array('in','10,20,30');
	    $condition['lock_state']=0;
	    $condition['buyer_id']=$this->member_info['member_id'];
	    //拿到订单的主题信息
	    $model_order=Model('order');
	    //代付款，
	    $wait_pay=$model_order->getOrderStateNewCount($condition);
	    //代发货，
	    $wait_send=$model_order->getOrderStatePayCount($condition);
	    //待收货，
	    $wait_receive=$model_order->getOrderStateSendCount($condition);
	    //待评价，
	    $wait_evaluate=$model_order->getOrderStateEvalCount($condition);
	    
	    $data['wait_pay']=$wait_pay;
	    $data['wait_send']=$wait_send;
	    $data['wait_receive']=$wait_receive;
	    $data['wait_evaluate']=$wait_evaluate;
	    //购物车，
	    $cart_model=Model('cart');
	    $type='db';
	    $condition_cart['buyer_id']=$this->member_info['member_id'];
	    $cart_count=$cart_model->getCartGoodsNum($type, $condition_cart);
	    $data['cart_count']=$cart_count['cart_goods_count'];

	    //优惠券，
	    $voucher_model=Model('voucher');
	    $member_id=$this->member_info['member_id'];
	    $voucher_count=$voucher_model->getCurrentAvailableVoucherCount($member_id);  
	    $data['voucher_count']=$voucher_count;

	    //我的拼团
	    $groupbuy_mem_model=Model('groupbuy_mem');
	    $condition_mem['member_id']=$this->member_info['member_id'];
	    $condition_mem['pay_status']=1;
	    $mem_count=$groupbuy_mem_model->get_mem_current_count($condition_mem);
	    $data['groupbuy_count']=$mem_count;
	    
	    //系统消息
	    $model_message=Model('message_center');
	    $condition_msg['msg_state']=0;
	    $msgNum=$model_message->getMsgCount($condition_msg);
	    $data['msgNum']=$msgNum;
	    output_data(array('msg_count' => $data));
	  
	    
	}

    /**
     * 我的商城
     */
	public function indexOp() {
        $member_info = array();
        $member_info['user_name'] = $this->member_info['member_name'];
        $member_info['avatar'] = getMemberAvatarForFilePath($this->member_info['member_avatar']);
        $member_info['point'] = $this->member_info['member_points'];
        $member_info['mobile'] = $this->member_info['member_mobile'];
        $member_info['predepoit'] = $this->member_info['available_predeposit'];
        $member_info['truename'] = $this->member_info['member_truename'];//2016-6-3陈雨楠||返回时添加真实姓名字段
        
        $condition = array();
        $condition['buyer_id'] = $this->member_info['member_id'];
        $condition['team_id'] = 0;
        $model_order = Model('order');
        $order_list_array = $model_order->getNormalOrderList($condition, 0, '*', 'order_id desc', '');
        $waitpay = 0;
        $waitsend = 0;
        $waitreceipt = 0;
        $waiteval = 0;
        foreach ($order_list_array as $value) {
            switch ($value['order_state']) {
                case 10:
                    $waitpay ++;
                    break;
                case 20:
                    $waitsend ++;
                    break;
                case 30:
                    $waitreceipt ++;
                    break;
                case 40:
                    $waiteval ++;
                    break;
            }
        }
        $order_num = array();
        $order_num['waitpay'] = $waitpay;
        $order_num['waitsend'] = $waitsend;
        $order_num['waitreceipt'] = $waitreceipt;
        $order_num['waiteval'] = $waiteval;
        
        output_data(array(
            'member_info' => $member_info,
            'order_num' => $order_num
        ));
        
        //output_data(array('member_info' => $member_info));
	   
	}

    public function modify_nicknameOp(){
        $member_id=$this->member_info['member_id'];
        $truename=$_POST['truename'];
        $member=Model('member');
        $where['member_id']=$member_id;
        $data['member_truename']=$truename;
        if($member->editMember($where,$data)){
            output_data(array('msg'=>'操作成功！'));
        }
        else{
            output_error('更新失败');
        }

    } 

    public function modify_passwordOp(){
        
        $member=Model('member');
        $member_id=$this->member_info['member_id'];
        $old_psw=$_POST['old_psw'];
        $new_psw=$_POST['new_psw'];
        $member_pass=$member->getMemberInfoByID($member_id,'member_passwd');
        /*if(md5(trim($old_psw))==$member_pass['member_passwd']){
            $where['member_id']=$member_id;
            $data['member_passwd']=md5(trim($new_psw));
            if($member->editMember($where,$data)){*/
        if(trim($old_psw)==$member_pass['member_passwd']){
            $where['member_id']=$member_id;
            $data['member_passwd']=trim($new_psw);
            if($member->editMember($where,$data)){
                output_data(array('msg'=>'操作成功！'));
            }else{
                output_error('更新失败');
            }
        }
        else{
            output_error('旧密码输入错误。');
        }
    }

    /**
     * 头像上传
     */
    public function avatar_uploadOp() {
        import('function.thumb');
        $member=Model('member');
        $member_id = $this->member_info['member_id'];
        //上传图片
        $upload = new UploadFile();
        $upload->set('thumb_width', 500);
        $upload->set('thumb_height',499);
        $ext = strtolower(pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION));
        $file_name = 'avatar_'.$member_id.'_'.time().'.'.$ext;
        $upload->set('file_name',$file_name);
        $upload->set('thumb_ext','_new');
        $upload->set('ifremove',true);
        $upload->set('default_dir',ATTACH_AVATAR);
        if (!empty($_FILES['avatar']['tmp_name'])){
            $result = $upload->upfile('avatar');
            if (!$result){
                output_error($upload->error);
            }else{
                $where['member_id']=$member_id;
                //$file_name=$file_name;
                $file_name = 'avatar_'.$member_id.'_'.time().'_new.'.$ext;
                $data['member_avatar']=$file_name;
                if($member->editMember($where,$data)){
                    output_data(array('msg'=>'操作成功！'));
                }
            }
        }else{
            output_error('上传失败，请尝试更换图片格式或小图片');
        }
    }
    
    /**
     * 积分日志列表
     */
    public function points_logOp(){
        $condition_arr = array();
        $condition_arr['pl_memberid'] = $this->member_info['member_id'];
        if ($_GET['stage']){
            $condition_arr['pl_stage'] = $_GET['stage'];
        }
        $condition_arr['saddtime'] = strtotime($_GET['stime']);
        $condition_arr['eaddtime'] = strtotime($_GET['etime']);
        if($condition_arr['eaddtime'] > 0) {
            $condition_arr['eaddtime'] += 86400;
        }
        $condition_arr['pl_desc_like'] = $_GET['description'];
        //分页
        $page   = new Page();
        $page->setEachNum(100);
        $page->setStyle('admin');
        //查询积分日志列表
        $points_model = Model('points');
        $list_log = $points_model->getPointsLogList($condition_arr,$page,'*','');
        output_data(array('list_log'=>$list_log));
    }

    //浏览记录
    public function browser_recordOp(){
        $browse=Model('goods_browse');
        
        $data=$browse->getViewedGoodsList($this->member_info['member_id'],3);
        output_data(array('browse_list'=>$data));
    }

    // 积分兑换
    public function points_buyOP(){
        $buyer_id = $this->member_info['member_id'];
        $goods_id = $_POST['goods_id'];
        $num = $_POST['num'];
        $address_id = $_POST['address_id'];
        
        $time = time();
        $buyer = Model('member')->where(array('member_id' => $buyer_id))->find();
        $goods = Model('goods')->where(array('goods_id' => $goods_id, 'is_trans' => 1))->find();
        $address = Model('address')->where(array('address_id'=>$address_id))->find();

        $amout_point = $num * $goods['trans_num'];

        if($amout_point <= $buyer['member_points']){
            $temp = $buyer['member_points'] - $amout_point;

            $aa = Model('member')->where(array('member_id' => $buyer_id))->update(array('member_points'=>$temp));

        }else{
            // 此处添加积分不足的提示
            echo "余额不足";
            exit();
        }

        // 订单表
        $order_data['order_sn'] = $time;
        $order_data['store_id'] = $goods['store_id'];
        $order_data['store_name'] = $goods['store_name'];
        $order_data['buyer_id'] = $buyer_id;
        $order_data['buyer_name'] = $buyer['member_name'];
        $order_data['buyer_email'] = $buyer['member_email'];
        $order_data['add_time'] = $time;
        $order_data['payment_code'] = 'point';
        $order_data['payment_time'] = $time;
        $order_data['order_state'] = 20;
        $order_data['order_from'] = 2;
        $order_id = Model()->table('order')->insert($order_data);
        
        if($order_id){
            // 订单商品表
            $order_goods_data['order_id'] = $order_id;
            $order_goods_data['goods_id'] = $goods_id;
            $order_goods_data['goods_name'] = $goods['goods_name'];
            $order_goods_data['goods_price'] = $goods['goods_price'];
            $order_goods_data['goods_num'] = $num;
            $order_goods_data['goods_image'] = $goods['goods_image'];
            $order_goods_data['goods_pay_price'] = 0;
            $order_goods_data['store_id'] = $goods['store_id'];
            $order_goods_data['buyer_id'] = $buyer_id;
            $order_goods_data['commis_rate'] = 0;
            $order_goods_data['gc_id'] = $goods['gc_id'];
            Model()->table('order_goods')->insert($order_goods_data);

            // order_common表
            $address_info['phone'] = $address['mob_phone'];
            $address_info['mob_phone'] = $address['mob_phone'];
            $address_info['tel_phone'] = $address['tel_phone'];
            $address_info['address'] = $address['area_info'].$address['address'];
            $address_info['area'] = $address['area_info'];
            $address_info['street'] = $address['address'];

            $order_common_data['order_id'] = $order_id;
            $order_common_data['store_id'] = intval($goods['store_id']);
            $order_common_data['daddress_id'] = $address_id ;
            $order_common_data['reciver_name'] = $address['true_name'];
            $order_common_data['reciver_info'] = serialize($address_info);
            $order_common_data['reciver_province_id'] = $address['province_id'];
            $order_common_data['reciver_city_id'] = $address['city_id'];
            
            Model()->table('order_common')->insert($order_common_data);

        }
        // 此处添加成功的提示
        echo "success";
    }

}